'use strict';

angular.module('app')
    .controller('realTimeMoniToController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster','$interval',
        function($scope, $rootScope, $state, $timeout, toaster,$interval) {

            $scope.title="实时监控";
            var id = $state.params.id; 
           // 初始化页面
            function activate(id) {
                $.ajax({
                    type: 'PUT',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    url : '/dac/bm/building/cblist',
                    data: angular.toJson({'id': id})
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        console.log(result.data);//默认
                        $scope.record = result.data;

                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            //7级地址下拉数据
            //市
            $scope.getaddress = function () {
                $scope.param = {
                    parentCode:440000//广东省
                };
                $.ajax({
                    type:'PUT',
                    dataType:'json',
                    contentType:'application/json;charset=UTF-8',
                    url:'/dic/queryDicListByParentId',
                    data: angular.toJson($scope.param)
                }).then(function (result) {
                    $scope.city = result.data;
                    $scope.$apply();
                })
            }
            $scope.getaddress();
            //区
            $scope.getarea = function () {
                var parentCode = $('#city').find("option:selected").data('code');//获取value的值
                console.log(parentCode);
               
                if(parentCode){
                    $scope.param = {
                        parentCode:parentCode//广州市区级
                    };
                    $.ajax({
                        type:'PUT',
                        dataType:'json',
                        contentType:'application/json;charset=UTF-8',
                        url:'/dic/queryDicListByParentId',
                        data: angular.toJson($scope.param)
                    }).then(function (result) {
                        if(result&&result!==undefined&&result!==''){
                            $scope.area = result.data;
                            //解除禁止选择的样式
                            $('#area').removeAttr('disabled');
                            $scope.$apply();
                        }
                        
                    })
                }
                
            }
            //乡镇/街道
            $scope.getStree = function () {
                var parentCode = $('#area').find("option:selected").data('code');//获取value的值
                console.log(parentCode);
                if(parentCode){
                    $scope.param = {
                        parentCode:parentCode//广州市区级
                    };
                    $.ajax({
                        type:'PUT',
                        dataType:'json',
                        contentType:'application/json;charset=UTF-8',
                        url:'/dic/queryDicListByParentId',
                        data: angular.toJson($scope.param)
                    }).then(function (result) {
                    	if(result&&result!==undefined&&result!==''){
                    		$scope.street = result.data;
    	                    //解除禁止选择的样式
    	                    $('#street').removeAttr('disabled');
                            $scope.$apply();
                    	}
                    })
                }
            }
            //路
            $scope.getRoad = function () {
                var parentCode = $('#street').find("option:selected").data('code');//获取value的值;
                console.log(parentCode)
                if(parentCode){
                    $scope.param = {
                        parentCode:parentCode//广州市区级
                    };
                    $.ajax({
                        type:'PUT',
                        dataType:'json',
                        contentType:'application/json;charset=UTF-8',
                        url:'/dic/queryDicListByParentId',
                        data: angular.toJson($scope.param)
                    }).then(function (result) {
                        if(result&&result!==undefined&&result!==''){
                    		$scope.road = result.data;
    	                    //解除禁止选择的样式
    	                    $('#road').removeAttr('disabled');
                            $scope.$apply();
                    	}
                    })
                }
            }
            //小区/村
            $scope.getQuarters = function () {
                var parentCode = $('#road').find("option:selected").data('code');//获取value的值;
                console.log(parentCode)
                if(parentCode){
                    $scope.param = {
                        parentCode:parentCode//广州市区级
                    };
                    $.ajax({
                        type:'PUT',
                        dataType:'json',
                        contentType:'application/json;charset=UTF-8',
                        url:'/dic/queryDicListByParentId',
                        data: angular.toJson($scope.param)
                    }).then(function (result) {
                       if(result&&result!==undefined&&result!==''){
                    		$scope.cunty = result.data;
    	                    //解除禁止选择的样式
    	                    $('#cunty').removeAttr('disabled');
                            $scope.$apply();
                    	}
                    })
                }
            }
            
            //小区/村子的设备
            $scope.hasData = false;
            
            $scope.getDevice = function () {
               var parentCode = $('#cunty').find("option:selected").data('code');//获取value的值;
               console.log(parentCode)
               if(parentCode){
                    $scope.param = {
                        id:parentCode
                    };
                    $.ajax({
                        type:'PUT',
                        dataType:'json',
                        contentType:'application/json;charset=UTF-8',
                        url:'/dac/bm/building/cblist',
                        data: angular.toJson($scope.param)
                    }).then(function (result) {
                        console.log(result);
                        if(result.data.length>0){
                            $scope.deviceList = result.data;
                            $scope.playing = false;
                            $scope.hasData = false;
                            $scope.$apply(function(){
                                // 设备列表渲染完成之后，向后台请求播放所需的videoKey以及视频地址url等信息
                                setTimeout(function(){
                                    var buildingIds = [];
                                    var liSpan = $('.cuntyUl li span');
                                    for(var i =0;i<liSpan.length;i++){
                                        buildingIds.push($(liSpan[i]).attr('data-buildingId'));
                                    }
                                    console.log(buildingIds);
                                    $.ajax({
                                        type:"put",
                                        dataType:"json",
                                        contentType:'application/json;charset=UTF-8',
                                        url:"/dac/bm/house/playVideo",
                                        data:angular.toJson({'buildingIds':buildingIds})
                                    }).then(function(result){
                                        console.log(result);
                                        var liSpan = $('.cuntyUl li span');
                                        if(result.data.length>0){
                                            for(var i =0;i<liSpan.length;i++){
                                                $(liSpan[i]).attr({'data-videoKey':result.data[i].videoKey,'data-rtspUrl':result.data[i].rtspUrl,'data-stopVideoUrl':result.data[i].stopVideoUrl});
                                            } 
                                            $scope.playing = true;
                                        }else{
                                            return;
                                        }
                                        $scope.$apply();
                                    })
                                },500)//延时500毫秒，主要是确保页面结构已经渲染
                           });
                        } else{
                            $scope.hasData = true;//提醒没有数据
                            //清空列表
                            $('.cuntyUl').html('');
                        } 
                        $scope.$apply(); 
                    })
                }
            }

            //设备名称选中并且播放
             $scope.timer = [];
            $('.cuntyUl').on('click','li',function(){
                var _this = $(this);
            	$(this).children('span').toggleClass('videoActive');
            	if($(this).children('span').hasClass('videoActive')){
                    $(this).children('span').html('关闭');
                    var _index = $(this).index();
                    $scope.timer.push(_index);
                    var rtspUrl = _this.children('span').attr('data-rtspUrl');
                    if(rtspUrl){
                        //动态给控件添加播放地址
                        var camerPlug = '<div class="item-f"> <object  classid="clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921"'
                        +'codebase="http://comic.sjtu.edu.cn/vlc/cab/axvlc.cab" id="vlc" name="vlc'
                        +'class="vlcPlayer" events="True">'
                        +'<param name="Src" value="'+rtspUrl+'" />'
                        +'<param name="ShowDisplay" value="True" />'
                        +'<param name="AutoLoop" value="False" />'
                        +'<param name="AutoPlay" value="True" />'
                        +'<param value="transparent" name="wmode">'
                        +'<embed id="vlcEmb" type="application/x-google-vlc-plugin"'
                        +'version="VideoLAN.VLCPlugin.2" autoplay="yes" loop="no"'
                        +'target="'+rtspUrl+' wmode="transparent"" ></embed></object></div>'

                        $('#liveVideo').append(camerPlug);

                        var num = ($('.data-right .item-f').length)//动态改变视频的宽高
                        if(num==1){
                            $(".data-right .item-f").css({'width':'96%','margin':'0 auto'});
                            $(".data-right .item-f object").css({'width':'100%','height':"600px"});
                        }
                        if(num>2&&num<=4){

                            $('.data-right .item-f').css({'width':'49.5%','float':'left'});
                            $(".data-right .item-f object").css({'width':'100%','height':"400px"});
                        }
                        if(num>4&&num<=9){
                            $('.data-right .item-f').css({'width':'33%','float':'left'});
                            $(".data-right .item-f object").css({'width':'100%','height':"400px"});
                        } 
                        
                        $scope.timer[_index]= $interval(function(){
                            var array = [];
                            var videoKey = _this.children('span').attr('data-videoKey');
                            array.push(videoKey);
                            // console.log(array)
                            videoActiveUrl(array);

                        },15000);
                       
                    }
                  
                }else{
                    $(this).children('span').html('播放');
                    var _index = $(this).index();
                    var array = [];
                    var videoKey = $(this).children('span').attr('data-videoKey');
                    array.push(videoKey);
                    $($(".data-right .item-f")[_index]).remove();
                    $interval.cancel($scope.timer[_index]);
                    for(var i= -1;i<$scope.timer.length;i++){
                        console.log(i);
                        console.log($scope.timer[i]);
                        if($scope.timer[i]==_index){
                            console.log('_index:'+i)
                        }
                    }
                    // stopActiveUrl(array);
                }
            });
            
            function videoActiveUrl(array){
                console.log(array)
                $.ajax({
                    url:'/dac/bm/house/sendVideoActive',
                    type:'put',
                    dataType:'json',
                    contentType:'application/json;charset=UTF-8',
                    data:angular.toJson({'videoKeys':array})
                }).then(function(res){
                    console.log(res)
                });

            }

            function stopActiveUrl(array){
                console.log(array)
                $.ajax({
                    url:'/dac/bm/house/stopvideo',
                    type:'put',
                    dataType:'json',
                    contentType:'application/json;charset=UTF-8',
                    data:angular.toJson({'videoKeys':array})
                }).then(function(res){
                    console.log(res)
                })
            }
           
        }]);

         